#pragma once
#ifndef LIBRARYSYSTEM_H
#define LIBRARYSYSTEM_H

#include "Book.h"
#include "Reader.h"
#include "BorrowRecord.h"
#include <vector>
#include <string>
#include <fstream>
#include <algorithm>
#include <cctype>
using namespace std;

// 链表节点模板
template<typename T>
struct Node {
    T data;
    Node* next;
    Node(const T& data) : data(data), next(nullptr) {}
};

class LibrarySystem {
private:
    Node<Book>* bookHead;
    Node<Reader>* readerHead;
    Node<BorrowRecord>* recordHead;
    int recordCounter;

    // 文件操作
    void loadBooksFromFile();
    void loadReadersFromFile();
    void loadRecordsFromFile();
    void saveBooksToFile();
    void saveReadersToFile();
    void saveRecordsToFile();

    // 链表操作 - 模板函数定义放在头文件中
    template<typename T>
    void addNode(Node<T>*& head, const T& data) {
        Node<T>* newNode = new Node<T>(data);
        if (!head) {
            head = newNode;
        }
        else {
            Node<T>* current = head;
            while (current->next) {
                current = current->next;
            }
            current->next = newNode;
        }
    }

    template<typename T>
    bool removeNode(Node<T>*& head, const T& data) {
        if (!head) return false;

        if (head->data == data) {
            Node<T>* temp = head;
            head = head->next;
            delete temp;
            return true;
        }

        Node<T>* current = head;
        while (current->next && !(current->next->data == data)) {
            current = current->next;
        }

        if (current->next) {
            Node<T>* temp = current->next;
            current->next = current->next->next;
            delete temp;
            return true;
        }

        return false;
    }

    template<typename T>
    T* findNode(Node<T>* head, const string& id) {
        Node<T>* current = head;
        while (current) {
            if constexpr (is_same_v<T, Book>) {
                if (current->data.getBookId() == id) {
                    return &(current->data);
                }
            }
            else if constexpr (is_same_v<T, Reader>) {
                if (current->data.getReaderId() == id) {
                    return &(current->data);
                }
            }
            else if constexpr (is_same_v<T, BorrowRecord>) {
                if (to_string(current->data.getRecordId()) == id) {
                    return &(current->data);
                }
            }
            current = current->next;
        }
        return nullptr;
    }

    template<typename T>
    vector<T> getAllNodes(Node<T>* head) {
        vector<T> result;
        Node<T>* current = head;
        while (current) {
            result.push_back(current->data);
            current = current->next;
        }
        return result;
    }

public:
    LibrarySystem();
    ~LibrarySystem();

    // 图书管理
    void addBook();
    void deleteBook();
    void modifyBook();
    void searchBooks();
    void displayAllBooks();

    // 读者管理
    void addReader();
    void deleteReader();
    void modifyReader();
    void searchReaders();
    void manageBlacklist();
    void displayAllReaders();

    // 借阅管理
    void borrowBook();
    void returnBook();
    void displayAllRecords();
    void displayOverdueRecords();

    // 查询功能
    void bookQuery();
    void readerQuery();
    void readerRecordQuery();

    // 系统功能
    void saveAllData();
    void loadAllData();
};

#endif
